package jsyf.oa.dao.attach;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import jsyf.oa.entity.attach.AttachModuleEntity;
import jsyf.oa.entity.attach.AttachmentEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by Administrator on 2017/12/13 0013.
 */
@Repository
public class AttachModuleDao extends BaseDao {


    @Autowired
    private AttachDao attachDao;

    public Integer addAttach(String attachId) {
        AttachmentEntity attachmentEntity = (AttachmentEntity) attachDao.getIBaseEntityById(attachId);
        attachmentEntity.setDelFlag(0);
       return attachDao.update(attachmentEntity);
    }

    public AttachModuleDao() {
        super.iBaseEntity = new AttachModuleEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, AttachModuleEntity.class);
    }

    public List<JSONObject> getByCondition(String condition, Object[] params) {
        String sql = "select * from V_COM_ATTACH where 1 = 1 " + condition;
        return super.queryForJsonList(sql.toString(), params);
    }

    public List<AttachModuleEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<AttachModuleEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            AttachModuleEntity entity = (AttachModuleEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }
}
